<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

	class SitController extends AdminBaseController
	{

		/*园区房间列表页*/	
		public function index(){
			// echo session('super_role_id');
			// exit;
		$where['s.status']=1;		
		$site_types = $this->request->param('site_type_id','');

		if($site_types){
			$where['site_type_id'] = $site_types;	
		}
		/**搜索条件**/
		$site_name = $this->request->param('site_name');
		$address = $this->request->param('address');

		if ($site_name) {
       	   $where['site_name'] = ['like', "%$site_name%"];
        }

        if ($address) {
            $where['s.address'] = ['like', "%$address%"];
        }
        if(session('incubator_id')){
        	$where['incubator_id']=session('incubator_id');
        }	
        $list=Db::name('site')->alias('s')->where($where)->join('incubator i','i.id=s.incubator_id')->field('s.id,s.incubator_id,s.site_name,s.site_type_id,s.price,s.address,s.people,s.user_name,s.mobile,i.tel,s.site_sketch,s.pic,s.sort,s.is_rent,s.start_time,s.end_time,s.status,i.pic_url,i.incubator_name,s.is_audit')->order('id','desc')->paginate(5);
    // echo get_cate_name(5,2);
    // exit;
        // foreach ($list as $key => $value) {
        	
        // 	# code...
        // }
        //   var_dump($list);
        // exit;
     //  echo Db::name('site')->getLastSql();
    
        $page=$list->render();
        $site_type=$this->site_type();
        $equipments=$this->equipments();
       // exit;
        // dump($site_type);
        // exit;
        $this->assign('equipments',$equipments);
        $this->assign('site_type_id',$site_types);
        $this->assign('site_name',$site_name);
        $this->assign('address',$address);
        $this->assign('site_type',$site_type);
        $this->assign("page", $page);

        $this->assign("list", $list);
        return $this->fetch();
    }



		/*园区添加*/		
		public function addSite(){
			//$_POST['start_time']='0000-00-00 00:00:00';
			if($_POST){
				if(!empty($_POST['equipments'])){
					$data['equipments'] =$_POST['equipments'];
				}
				if(is_array($data['equipments'])){
					$data['equipments']=implode(',', $data['equipments']);
				}
				$data['site_name'] = $this->request->param('site_name','');
				$data['incubator_id'] = $this->request->param('incubator_id',0,'intval');
				$data['site_type_id'] = $this->request->param('site_type_id',1);
				$data['price'] = $this->request->param('price',0);
				$data['mobile'] = $this->request->param('mobile','');
				$data['people'] = $this->request->param('people',0,'intval');	
			//场地正常一天开始到结束的时间
				
				// echo $time;
				// exit;
				$start_times = $this->request->param('start_times','');
				$start_timee= $this->request->param('start_timee','');
				$data['start_time'] = $start_times.':'.$start_timee;
				$end_times = $this->request->param('end_times','');
				$end_timee = $this->request->param('end_timee','');
				$data['end_time'] = $end_times.':'.$end_timee;
				$data['pic'] = $this->request->param('pic','');
				$data['pic'] = $this->request->param('pic','');
				$data['site_sketch'] = $this->request->param('site_sketch','');
				$data['address'] = $this->request->param('address');
				$data['status'] = $this->request->param('status',1);
				$data['sort'] = $this->request->param('sort',0);
				if(!$data['start_time']){
					unset($data['start_time']);
				}
				if(!$data['end_time']){
					unset($data['end_time']);
				}
			// echo $incubator_name ;
			// if($type_id==1){
			// 	$station_	
			// }					

			// echo $type_id;
	// var_dump($data);
	// 			exit;
				$add=Db::name('site')->insert($data);

				if($add){
					$this->success('添加成功','sit/index');
				}else{
					$this->error('添加失败');
				}	
			}else{
				$where=array();	
				if(session('incubator_id')){
					$where['id']=session('incubator_id');
				}
				$incubator_list=db('incubator')->where(array('status'=>1))->where($where)->field('id,incubator_name')->select();
				$site_type=$this->site_type();
				$equipments=$this->equipments();
                $this->assign('equipments',$equipments);
				$this->assign('site_type',$site_type);
				$this->assign('incubator_list',$incubator_list);
				return $this->fetch();
			}
			





		}
		/*场地类型*/
		function site_type(){
			$where=array('t.name'=>'场地类型');
			$list=db("category_type")->alias('t')->join('category c','t.id=c.type_id')->field('c.id,c.name')->where($where)->select();
			return $list;
		}
		/*设备s*/
		function equipments(){
			// echo round(365/2,2);
			$where=array('t.name'=>'设备');
			$list=db("category_type")->alias('t')->join('category c','t.id=c.type_id')->field('c.id,c.name')->where($where)->select();
			return $list;
		}
		/*详情*/
		public function detailSite(){
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['cx_site.id']= $id;
			}else{
				// dump(info_back('','id为空',false));
				// exit;
				return info_back('','id为空',false);
			}
		//	$detail= Db::name('site')->where($where)->join('cx_incubator','cx_site.incubator_id=cx_incubator.id')->find();
			$detail= Db::name('site')->where($where)->find();
			//var_dump($detail);
			$where=array();	
			if(session('incubator_id')){
				$where['id']=session('incubator_id');
			}
			$incubator_list=db('incubator')->where(array('status'=>1))->where($where)->field('id,incubator_name')->select();
			
			$site_type=$this->site_type();



			$equipments_checked=explode(',', $detail['equipments']);
			$this->assign('equipments_checked',json_encode($equipments_checked));
			$equipments=$this->equipments();
			$this->assign('equipments',$equipments);
			$this->assign('site_type',$site_type);
			$this->assign('incubator_list',$incubator_list);
			$this->assign('detail',$detail);
			return $this->fetch();
		}

		/*编辑园区*/	
		public function editSite(){
			if($_POST){
				$_POST['start_time']=$_POST['start_times'].':'.$_POST['start_timee'];
				$_POST['end_time']=$_POST['end_times'].':'.$_POST['end_timee'];
				unset($_POST['start_times']);
				unset($_POST['start_timee']);
				unset($_POST['end_times']);
				unset($_POST['end_timee']);
				if(!empty($_POST['equipments'])){
					$data['equipments'] =$_POST['equipments'];
				}
				if(is_array($data['equipments'])){
					$_POST['equipments']=implode(',', $data['equipments']);
				}
				$edit = Db::name('site')->update($_POST);
				if($edit){
					$this->success('编辑成功','Sit/index');
				}else{
					$this->success('编辑成功','Sit/index');
				}
			}
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['id']= $id;
			}else{
				$this->error('该信息id不能为空');
			}
			// echo $id;
			// exit;
			//dump($where);
			$detail= Db::name('site')->where($where)->find();
			$start_time=explode(':', $detail['start_time']);
			$detail['start_times']=$start_time[0];
			$detail['start_timee']=$start_time[1];
			$end_time=explode(':', $detail['end_time']);
			$detail['end_times']=$end_time[0];
			$detail['end_timee']=$end_time[1];
			//dump($detail);
			//exit;
			$where=array();	
			if(session('incubator_id')){
				$where['id']=session('incubator_id');
			}

			$incubator_list=db('incubator')->where(array('status'=>1))->where($where)->field('id,incubator_name')->select();
			$equipments_checked=explode(',', $detail['equipments']);
			$this->assign('equipments_checked',json_encode($equipments_checked));
			$site_type=$this->site_type();
			$equipments=$this->equipments();
			$this->assign('equipments',$equipments);
			$this->assign('site_type',$site_type);
			$this->assign('incubator_list',$incubator_list);
			$this->assign('detail',$detail);
			return $this->fetch();

		}


		/*删除园区*/
		public function delSite(){
			$data['id']= $this->request->param('id',0,'intval');

			$data['status']=0;

			$del=Db::name('site')->update($data);
			if($del){
				return 1;
			}else{
				return 0;
			}

		}	

		/*预约审核*/	
		public function audit_Site(){
			$data['id']= $this->request->param('id',0,'intval');
			if($_POST['cancel']==1){
				$data['is_audit']=1;
			}else{
				$data['is_audit']=2;
			}
			

			$del=Db::name('site')->update($data);
			if($del){
				return 1;
			}else{
				return 0;
			}

		}		





































}